package com.example.myapplication22.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.myapplication22.entity.Info;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyHelper extends SQLiteOpenHelper {

    public MyHelper(Context context){
        super(context,"lhj.db",null,1);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {//数据库第一次创建时调用
        /*sqLiteDatabase.execSQL("create table user" +
                "(_id interger primary key autoincrement) ");*/
        sqLiteDatabase.execSQL("CREATE TABLE \"user\" (\n" +
                "  \"_id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
                "  \"userId\" text NOT NULL,\n" +
                "  \"password\" TEXT NOT NULL,\n" +
                "  \"img\" integer,\n" +
                "  \"sex\" TEXT,\n" +
                "  \"date\" TEXT\n" +
                ")");
        sqLiteDatabase.execSQL("CREATE TABLE \"info\" (\n" +
                "  \"_id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
                "  \"userId\" text NOT NULL,\n" +
                "  \"title\" TEXT,\n" +
                "  \"content\" TEXT,\n" +
                "  \"week\" TEXT,\n" +
                "  \"date\" TEXT,\n" +
                "  \"state\" integer\n" +
                ")");

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    //更新
    public  boolean getUpdate(String title,String content,String date,String _id){
        SQLiteDatabase w = MyHelper.this.getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put("title",title);
        value.put("content",content);
        value.put("date",date);
        int i = w.update("info", value, "_id=?", new String[]{_id});
        return i>0?true:false;
    }
    //添加
    public boolean getInsert(Info info){
        SQLiteDatabase w = MyHelper.this.getWritableDatabase();

        ContentValues value = new ContentValues();
        value.put("userId",info.getUserId());
        value.put("title",info.getTitle());
        value.put("content",info.getContent());
        value.put("date",info.getDate().toString());
        long i = w.insert("info", null, value);
        return i>0?true:false;
    }
}
